from research.calcor.calcor_logic import logic_calc_dict
class measure_count:
    def __init__(self,auto_logic=[],chain_keys={},):
        self.auto_logic=auto_logic
        self.chain_keys=chain_keys
        self.reset()
    def reset(self):
        self.data={}
        self.chaindata={}
        for i in self.auto_logic.keys():
            self.data[i]=0
        for k,v in self.chain_keys.items():
            self.chaindata[k]={}
            key=""
            for i in v:
                key=key+"_"+i
                self.chaindata[k][key]=0
    def auto_count(self,b):
        rr={}
        for k,v in self.auto_logic.items():
            if isinstance(v,dict):
                r = logic_calc_dict(v, b)
            else:
                r=v(b[k],b)
            rr[v]=r
            if r:
                self.data[k]=self.data[k]+1
        for k,v in self.chain_keys.items():
            key = ""
            r0=[]
            for vv in v:
                r0.append(rr[vv])
                key = key + "_" + vv
                if False not in r0:
                    self.chaindata[k][key] = self.chaindata[k][key]+1
    def other_count(self,key,n=1):
        if key in self.data.keys():
            self.data[key]=self.data[key]+n
        else:
            self.data[key] =n